package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.specification.SpecVo;
import com.dk.pojo.specification.TbSpecification;
import com.dk.pojo.specification.TbSpecificationOption;
import com.dk.pojo.template.TbTypeTemplate;
import com.dk.service.specification.SpecificationService;
import com.dk.service.template.TemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@CrossOrigin
@Controller
@RequestMapping("specification")
public class SpecificationController {

    private final Logger logger = LoggerFactory.getLogger(SpecificationController.class);

    @Reference
    private SpecificationService specificationService;

    @Reference
    private TemplateService templateService;

    @RequestMapping("toSpecification")
    public String toSpecification() {
        return "admin/specification";
    }

    @RequestMapping("listSpecification")
    @ResponseBody
    public Result listSpecification() {
        try {
            List<TbSpecification> list = specificationService.listSpecification();
            return Result.response(ResultEnum.OK).setData(list);

        } catch (Exception e) {
            logger.error("获取品牌列表错误：",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("addSpecification")
    @ResponseBody
    public Result addSpecification (String specName,String options) {
        try {
            int i = specificationService.addSpecificationAndOptions(specName,options);
            return Result.response(ResultEnum.OK);

        }catch (Exception e) {
            logger.error("规格添加错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
    @RequestMapping("getSpecificationAndOption/{id}")
    @ResponseBody
    public Result getSpecificationAndOption(@PathVariable("id") Long id) {
        try {
            Map<String,Object> map = specificationService.getSpecificationAndOption(id);
            return Result.response(ResultEnum.OK).setData(map);
        }catch (Exception e){
            logger.error("查询规格表、选项表错误：",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("updateSpecificationInfo")
    @ResponseBody
    public Result updateSpecificationInfo(TbSpecification tbSpecification,String options) {
        try {
            if (tbSpecification.getId() != null) {
                int i = specificationService.updateSpecificationInfo(tbSpecification,options);
                return Result.response(ResultEnum.OK);
            } else {
                return Result.response(ResultEnum.FAIL);
            }
        } catch (Exception e) {
            logger.error("规格修改错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }


    @RequestMapping("deleteSpecificationByIds")
    @ResponseBody
    public Result deleteSpecificationByIds(@RequestParam("ids[]")Long[] ids) {
        try {
            int i = specificationService.deleteSpecificationByIds(ids);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("品牌删除错误:",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("getSpecAndOptionByTypeId")
    @ResponseBody
    public Result getSpecAndOptionByTypeId(@RequestParam("templateId") Long templateId) {
        try {
            TbTypeTemplate template = templateService.getTemplateById(templateId);
            List<SpecVo> specVoList = JSONObject.parseArray(template.getSpecIds(), SpecVo.class);
            for (SpecVo specVo : specVoList) {
                List<TbSpecificationOption> options = specificationService.listOptionBySpecId(specVo.getId());
                specVo.setOptions(options);
            }
            return Result.response(ResultEnum.OK).setData(specVoList);
        } catch (Exception e) {
            logger.error("getSpecAndOptionByTypeId方法出现异常", e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
